<template>
  <view class="brand-detail-container">
    <!-- 品牌详情内容 -->
    <view class="detail-content" v-if="brandDetail">
      <!-- 品牌基本信息 -->
      <view class="basic-info">
        <text class="brand-name">{{ brandDetail.name }}</text>
      </view>
      
      <!-- 品牌详细信息列表 -->
      <view class="detail-list">
        <view class="detail-item">
          <text class="detail-label">品牌ID：</text>
          <text class="detail-value">{{ brandDetail.id }}</text>
        </view>
        
        <view class="detail-item">
          <text class="detail-label">所属店铺：</text>
          <text class="detail-value">{{ brandDetail.shop_id }}</text>
        </view>
        
        <view class="detail-item">
          <text class="detail-label">权重：</text>
          <text class="detail-value">{{ brandDetail.weigh }}</text>
        </view>
        
        <view class="detail-item">
          <text class="detail-label">状态：</text>
          <text class="detail-value">{{ getStatusText(brandDetail.status) }}</text>
        </view>
        
        <view class="detail-item">
          <text class="detail-label">创建时间：</text>
          <text class="detail-value">{{ formatDateTime(brandDetail.createtime) }}</text>
        </view>
        
        <view class="detail-item" v-if="brandDetail.updatetime">
          <text class="detail-label">更新时间：</text>
          <text class="detail-value">{{ formatDateTime(brandDetail.updatetime) }}</text>
        </view>
      </view>
      
      <!-- 操作按钮 -->
      <view class="action-buttons">
        <button class="edit-btn" @click="handleEdit">编辑品牌</button>
        <button class="delete-btn" @click="handleDelete">删除品牌</button>
      </view>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-state" v-if="loading">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 错误状态 -->
    <view class="error-state" v-if="errorMsg">
      <text class="error-text">{{ errorMsg }}</text>
      <button class="retry-btn" @click="getBrandDetail">重试</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      brandId: '',
      brandDetail: null,
      loading: false,
      errorMsg: ''
    };
  },
  onLoad(options) {
    if (options.id) {
      this.brandId = options.id;
      this.getBrandDetail();
    } else {
      this.errorMsg = '缺少品牌ID';
    }
  },
  methods: {
    // 获取品牌详情
    async getBrandDetail() {
      try {
        this.loading = true;
        this.errorMsg = '';
        const res = await this.$request({
          url: '/table/brand/edit',
          method: 'GET',
          params: {
            ids: this.brandId
          }
        });
        
        if (res.code === 1) {
          this.brandDetail = res.data;
        } else {
          throw new Error(res.msg || '获取品牌详情失败');
        }
      } catch (error) {
        console.error('获取品牌详情失败:', error);
        this.errorMsg = error.message || '获取品牌详情失败';
      } finally {
        this.loading = false;
      }
    },
    
    // 获取状态文本
    getStatusText(status) {
      return status === 'normal' ? '正常' : '禁用';
    },
    
    // 格式化日期时间
    formatDateTime(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
    },
    
    // 返回上一页
    handleBack() {
      uni.navigateBack();
    },
    
    // 编辑品牌
    handleEdit() {
      uni.navigateTo({
        url: `/pages/tablebrand/components/brandedit?id=${this.brandId}`
      });
    },
    
    // 删除品牌
    handleDelete() {
      uni.showModal({
        title: '确认删除',
        content: `确定要删除品牌 "${this.brandDetail?.name}" 吗？`,
        success: async (res) => {
          if (res.confirm) {
            try {
              const result = await this.$request({
                url: '/table/brand/del',
                method: 'POST',
                data: {
                  ids: this.brandId
                }
              });
              
              if (result.code === 1) {
                uni.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 延迟返回上一页，让用户看到删除成功的提示
                setTimeout(() => {
                  uni.navigateBack();
                }, 1500);
              } else {
                throw new Error(result.msg || '删除失败');
              }
            } catch (error) {
              console.error('删除品牌失败:', error);
              uni.showToast({
                title: error.message || '删除失败',
                icon: 'none'
              });
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.brand-detail-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding: 30rpx;
}

/* 品牌详情内容 */
.detail-content {
  width: 100%;
}

/* 品牌基本信息 */
.basic-info {
  background-color: #fff;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  text-align: center;
}

.brand-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

/* 品牌详细信息列表 */
.detail-list {
  background-color: #fff;
  border-radius: 15rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx 30rpx;
  border-bottom: 1rpx solid #eee;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-label {
  font-size: 28rpx;
  color: #666;
}

.detail-value {
  font-size: 28rpx;
  color: #333;
  text-align: right;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 30rpx;
  padding: 30rpx 0;
}

.edit-btn {
  flex: 1;
  padding: 28rpx 0;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 15rpx;
  font-size: 32rpx;
  font-weight: bold;
}

.delete-btn {
  flex: 1;
  padding: 28rpx 0;
  background-color: #ff3b30;
  color: #fff;
  border: none;
  border-radius: 15rpx;
  font-size: 32rpx;
  font-weight: bold;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 8rpx solid #f3f3f3;
  border-top: 8rpx solid #4cd964;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 30rpx;
}

.loading-text {
  font-size: 32rpx;
  color: #666;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误状态 */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
  text-align: center;
}

.error-text {
  font-size: 32rpx;
  color: #ff3b30;
  margin-bottom: 40rpx;
  padding: 0 40rpx;
}

.retry-btn {
  padding: 20rpx 60rpx;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 30rpx;
  font-size: 28rpx;
}
</style>